jQuery(document).ready(function( $ ) {
	/***
	 * Function name -- Add new field data
	 */
	$("input[name^='add_field_data-']").live("click",function() {
		
		var ID = $(this).attr("name").split("-"); // Get the id each clicked element
		
		var data_field_value = $("input[name='field_data-"+ID[1]+"']").val(); // get the data value
		
		if(data_field_value.trim() == "") { // Essential Validation
			alert("Please enter the input content before you add a new item");
		} else {
			var count_data = $(".ul_data_list-"+ID[1]+" li").length; // count the list of data
	
			$(".ul_data_list-"+ID[1]+"").append("<li>"+data_field_value+" - <a href='javascript:void(0)' name=remv-"+count_data+">Remove this item</a></li>"); // Add the item to ul tag
			$(".ul_data_list-"+ID[1]+"").append("<input id=data-"+count_data+" type='hidden' name='data-"+ID[1]+"[]' value='"+data_field_value+"' />");  // Add the hidden form
			$("input[name='field_data-"+ID[1]+"']").val(""); // Clear the value in the data name field
			count_data++;
		}
		
		
		
	});
	
	/***
	 * This function is fire when the user click "remove this item" link.
	 */
	$("ul[class^='ul_data_list-'] li a").live("click",function() {
		
                var get_ul_class = $(this).parent("li").parent("ul").attr("class");
                var retrive_id = get_ul_class.split("-");
                
		var ClickedLinkName = $(this).attr("name"); // get the link name
		
		var ID = ClickedLinkName.split("-"); // split to archive the ID
              
		$(".ul_data_list-"+retrive_id[1]+" input[id='data-"+ID[1]+"']").remove(); // Remove the hidden input
		
		
		$(this).parent("li").remove(); // remove the whole li
	});
		
	/***
	 * Show / Hidden field
	 */
	$("a[name='hide_form']").live("click",function(){
		var text = $(this).text();
		if($(this).next().attr("style")) {
			$(this).next().removeAttr("style");
		} else {
			$(this).next().css("display","none");
		}
	});	
});	

